import common from './common.js'
var ua = window.navigator.userAgent;
var isAndroid = ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1; //android终端
var isiOS = !!ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//跳转进入在麦房间
export const jumpInterRoom = (state, roomId, roomType) => {
    if (state != 2) return;
    if (parseInt(roomId) > 0) {
        if (isAndroid) {
            window.location = 'guagua://qiqi::1::1:' + roomId + ';30:' + roomType;
        } else if (isiOS) {
            window.location = '#guagua://qiqi::1::1:' + roomId + ';30:' + roomType;
        } else {
            if (!common.obj.anchorId) {
                location.href = 'tvshow:roomid=' + roomId + '&' + roomType
            } else {
                location.href = "http://ttcmd.qxiu.com/game/enter?roomId=" + roomId;
            }
        }
    }
}
// 滚动加载
export const getMore = (e, obj) => {
    if (obj.loading) {
        let clientHeight2 = e.target.clientHeight;
        let scrollTop2 = e.target.scrollTop;
        let scrollHeight2 = e.target.scrollHeight;
        if ((scrollTop2 + clientHeight2 + 1) >= scrollHeight2) {
            obj.callFun()
            // console.log(obj.callFun);
        }
    }
}

// 点击回到顶部
export const goTop = (_id) => {
    if (!_id) return;

    document.querySelector('#' + _id).scrollIntoView({
        behavior: "smooth", // 平滑过渡
        block: "start" // 上边框与视窗顶部平齐。默认值
    });
}

export const subString = function (str, len, hasDot) {
    if(null == str || undefined == str){
        str = '';
    }
    str = str.replace(/&nbsp;/g, ' ');
    var newLength = 0;
    var newStr = "";
    var chineseRegex = /[^\x00-\xff]/g;
    var singleChar = "";
    var strLength = str.replace(chineseRegex, "**").length;

    for (var i = 0; i < strLength; i++) {
        singleChar = str.charAt(i).toString();
        if (singleChar.match(chineseRegex) != null) {
            newLength += 2;
        } else {
            newLength++;
        }
        if (newLength > len) {
            break;
        }
        newStr += singleChar;
    }
    if (hasDot && strLength > len) {
        newStr += "...";
    }
    return newStr;
};